package com.local.es.rabbitmq;

import com.local.es.constants.ESConstants;
import com.local.es.service.GoodsService;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 商品消息消费者类，负责监听商品队列，并处理消息
 */
@Component
public class GoodsConsumer {

    @Autowired
    private GoodsService goodsService;

    /**
     * 监听es-demo.goods-update-queue队列，如果队列不存在，则会自动创建队列
     * 获取到消息goodsId之后，调用方法更新es中对应的文档
     * @param goodsId
     */
    @RabbitListener(queuesToDeclare = @Queue(ESConstants.ES_GOODS_UPDATE_QUEUE))
    public void goodsUpdateQueueListener(Integer goodsId) {
        System.out.println("开始更新goodsId："+goodsId);
        Integer updateResult = goodsService.updateES(goodsId);
        System.out.println("更新完成goodsId："+goodsId);
    }


    /**
     * 监听es-goods-delete-queue队列，如果队列不存在，则会自动创建队列
     * 获取到消息goodsId之后，调用方法删除es中对应的文档
     * @param goodsId
     */
    @RabbitListener(queuesToDeclare = @Queue(ESConstants.ES_GOODS_DELETE_QUEUE))
    public void goodsDeleteQueueListener(Integer goodsId) {
        System.out.println("开始删除goodsId："+goodsId);
        goodsService.deleteES(goodsId);
        System.out.println("删除完成goodsId："+goodsId);
    }
}
